﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ITunesSolver.Helpers;
using System.IO;
using System.Collections.ObjectModel;
using ITunesSolver.Helpers.DataVirtualization;
using System.Threading;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Threading;
using ITunesSolver.Data;

namespace ITunesSolver.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : BaseWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            ITunesLibraryA = new ITunesLibrary();
            ITunesLibraryB = new ITunesLibrary();

            //List<string> keys = ITunesHelper.GetITunesTrackKeys(@"C:\Users\hstirling\Documents\BENNEVIS iTunes Music Library.xml");
            //List<string> keys = ITunesHelper.GetITunesPlaylistKeys(@"C:\Users\hstirling\Documents\BENNEVIS iTunes Music Library.xml");

            //File.WriteAllLines("c:\\temp\\keys.txt", keys.ToArray());

            //ITunesHelper.LoadITunesLibrary(@"C:\Users\hstirling\Documents\Original_iTunesMusicLibrary.xml");
            //ITunesHelper.SaveITunesLibrary(@"c:\temp\itunestest.xml");

            //InitializeControls();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
        }

        private void miFile_Load_Click(object sender, RoutedEventArgs e)
        {
            Trace.WriteLine("miFile_Load_Click START");

            LoadFilesWindow loadFilesForm = new LoadFilesWindow();
            loadFilesForm.FileALoadEvent += new LoadFilesWindow.FileALoadDelegate(loadFilesForm_FileALoadEvent);
            loadFilesForm.FileBLoadEvent += new LoadFilesWindow.FileBLoadDelegate(loadFilesForm_FileBLoadEvent);
            loadFilesForm.FilesLoadEvent += new LoadFilesWindow.FilesLoadDelegate(loadFilesForm_FilesSelectedEvent);
            loadFilesForm.Owner = this;
            loadFilesForm.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            loadFilesForm.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            loadFilesForm.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            loadFilesForm.ShowDialog();

        //    ////string fileName = string.Empty;
        //    ////fileName = FileHelper.SelectFile("iTunes Music Library.xml");

        //    ////Thread thread = new Thread(LoadITunesLibrary);
        //    ////thread.SetApartmentState(ApartmentState.MTA);
        //    ////thread.Start(fileName);

        //    ////old stuff
        //    ////bool result = ITunesHelper.LoadITunesLibrary(fileName);

        //    ////InitializeControls();

        //    ////lvTracks.UpdateLayout();
        //    ////lvPlaylists.UpdateLayout();

            Trace.WriteLine("miFile_Load_Click DONE");
        }

        void loadFilesForm_FileBLoadEvent(object sender, string fileNameB)
        {
            ITunesLibraryB.FileName = fileNameB;
            topLevelTabs.Initialize(ITunesLibraryA, ITunesLibraryB);
        }

        void loadFilesForm_FileALoadEvent(object sender, string fileNameA)
        {
            ITunesLibraryA.FileName = fileNameA;
            topLevelTabs.Initialize(ITunesLibraryA, ITunesLibraryB);
        }

        void loadFilesForm_FilesSelectedEvent(object sender, string fileNameA, string fileNameB)
        {
            ITunesLibraryA.FileName = fileNameA;
            ITunesLibraryB.FileName = fileNameB;
            topLevelTabs.Initialize(ITunesLibraryA, ITunesLibraryB);
        }

        private void miFile_Combine_Click(object sender, RoutedEventArgs e)
        {

        }

        private void miTrack_Match_Click(object sender, RoutedEventArgs e)
        {
            if (ITunesLibraryA.FileName == null || ITunesLibraryB.FileName == null || ITunesLibraryA.FileName == string.Empty || ITunesLibraryB.FileName == string.Empty) 
            {
                MessageBox.Show("You must load files first.");
                return; 
            }

            MatchTracksWindow matchTracksWindow = new MatchTracksWindow();
            matchTracksWindow.Owner = this;
            matchTracksWindow.ShowDialog();

        }

        private void miTrack_Combine_Click(object sender, RoutedEventArgs e)
        {

        }

        private void miFile_Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
